<%-- 
    Document   : PublicProfile
    Created on : Dec 9, 2011, 9:15:24 AM
    Author     : hendrikde
--%>

<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Collections" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Public Profile</title>
    </head>
    <body>
        <input type="button" value="Return" onclick="window.location='gohome.jsp'"/>
        <%
        int user_id = Integer.parseInt(request.getParameter("PERS"));
        
        DBClasses.GetConnection gc = new DBClasses.GetConnection();
        java.sql.Connection conn = gc.getConnection();
        
        try {
            /* Remember
            * 0--Created Circle
            * 1--Join Circle
            * 2--Post
            * 3--Comment
            * 4--Like Post
            * 5--Like Comment
            */
            
            java.sql.Statement stmtPre = conn.createStatement();
            String queryPre = "SELECT U.Firstname, U.Lastname "
                    + "FROM Customer U "
                    + "WHERE U.CustomerID=" +user_id;
            java.sql.ResultSet rsPre = stmtPre.executeQuery(queryPre);
            
            rsPre.next();
            
            String fname=rsPre.getString(1);
            String lname=rsPre.getString(2);
            
            java.sql.Statement stmt0 = conn.createStatement();
            String query0 = "SELECT * "
                    + "FROM Circle I "
                    + "WHERE I.CircleOwner=" + user_id;
            java.sql.ResultSet rs0 = stmt0.executeQuery(query0);
            java.sql.Statement stmt1 = conn.createStatement();
            String query1 = "SELECT * "
                    + "FROM Circlemembership C "
                    + "WHERE C.CustomerID ="+ user_id;
            java.sql.ResultSet rs1=stmt1.executeQuery(query1);
            
            java.sql.Statement stmt2 = conn.createStatement();
            String query2 = "SELECT * "
                    + "FROM Posts P "
                    + "WHERE P.Author=" + user_id;
            java.sql.ResultSet rs2=stmt2.executeQuery(query2);
            
            java.sql.Statement stmt3 = conn.createStatement();
            String query3 = "SELECT * "
                    + "FROM Comments M "
                    + "WHERE M.Author=" + user_id;
            java.sql.ResultSet rs3 = stmt3.executeQuery(query3);
            
            java.sql.Statement stmt4 = conn.createStatement();
            String query4 = "SELECT * "
                    + "FROM post_likes L "
                    + "WHERE L.CustomerID=" +user_id;
            java.sql.ResultSet rs4 = stmt4.executeQuery(query4);
            
            java.sql.Statement stmt5 = conn.createStatement();
            String query5 = "SELECT * "
                    + "FROM comment_likes K "
                    + "WHERE K.CustomerID=" + user_id;
            java.sql.ResultSet rs5 = stmt5.executeQuery(query5);
            
            ArrayList<DBClasses.HistoryNode> hn = new ArrayList<DBClasses.HistoryNode>();
            
            while(rs0.next()) {
                String d = rs0.getString(5);
                String[] data = new String[3];
                //Data Stored: CircleName, CircleType, Date
                data[0] = rs0.getString(2);
                data[1] = rs0.getString(3);
                data[2] = d;
                
                hn.add(new DBClasses.HistoryNode(0, data, d));
            }
            
            while (rs1.next()) {
                String d = rs1.getString(3);
                String[] data = new String[4];
                //Data Stored: CircleName, CircleType, CircleOwner, Date
                java.sql.Statement stmtIntern = conn.createStatement();
                String queryIntern = "SELECT C.CircleName, C.CircleType, CONCAT(U.FirstName, ' ', U.LastName) "
                        + "FROM Circle C, Customer U "
                        + "WHERE C.CircleID='" + rs1.getString(2) +"' AND U.CustomerID=C.CircleOwner";
                java.sql.ResultSet rsIntern = stmtIntern.executeQuery(queryIntern);
                rsIntern.next();
                data[0] = rsIntern.getString(1);
                data[1] = rsIntern.getString(2);
                data[2] = rsIntern.getString(3);
                data[3] = d;
                hn.add(new DBClasses.HistoryNode(1, data, d));
                
            }
            
            while (rs2.next()) {
                String d = rs2.getString(2);
                String[] data = new String[4];
                //Data Stored: Content, AssocCircle Name, AssocCircle Owner, Date
                String pgid = rs2.getString(6);
                java.sql.Statement stmtIntern1 = conn.createStatement();
                String queryIntern1 = "SELECT C.CircleName, CONCAT(U.FirstName, ' ', U.LastName) "
                        + "FROM Circle C, Customer U "
                        + "WHERE U.CustomerID = C.CircleOwner AND C.CircleID IN "
                        + "(SELECT G.AssociatedCircleID "
                        + "FROM Pages G "
                        + "WHERE G.PageID='" +pgid + "')";
                java.sql.ResultSet rsIntern1 = stmtIntern1.executeQuery(queryIntern1);
                rsIntern1.next();
                data[0] = rs2.getString(3);
                data[1] = rsIntern1.getString(1);
                data[2] = rsIntern1.getString(2);
                data[3] = d;
                hn.add(new DBClasses.HistoryNode(2, data, d));
            }
            
            while (rs3.next()) {
                String d = rs3.getString(2);
                String[] data = new String[4];
                //Data Stored: Content, Post Author, Circle Name, Date
                String pid = rs3.getString(5);
                java.sql.Statement stmtIntern2 = conn.createStatement();
                String queryIntern2 = "SELECT C.CircleName, CONCAT(U.FirstName, ' ', U.LastName) "
                        + "FROM Circle C, Customer U "
                        + "WHERE U.CustomerID IN "
                        + "(SELECT P.Author "
                        + "FROM Posts P "
                        + "WHERE P.PostID='" +pid +"') "
                        + "AND "
                        + "C.CircleID IN "
                        + "(SELECT G.AssociatedCircleID "
                        + "FROM Pages G "
                        + "WHERE G.PageID IN "
                        + "(SELECT O.PageID "
                        + "FROM Posts O "
                        + "Where O.PostID='" + pid +"'))";
                
                java.sql.ResultSet rsIntern2 = stmtIntern2.executeQuery(queryIntern2);
                rsIntern2.next();
                data[0] = rs3.getString(3);
                data[1] = rsIntern2.getString(2);
                data[2] = rsIntern2.getString(1);
                data[3] = d;
                
                hn.add(new DBClasses.HistoryNode(3, data, d));
            }
            
            while (rs4.next()) {
                String d = rs4.getString(3);
                String[] data = new String[2];
                //Data Stored: Post Author, Date
                String pid = rs4.getString(1);
                
                java.sql.Statement stmtIntern3 = conn.createStatement();
                String queryIntern3 = "SELECT CONCAT(U.Firstname, ' ', U.Lastname) "
                        + "FROM Customer U "
                        + "WHERE U.CustomerID IN "
                        + "(SELECT P.Author "
                        + "FROM Posts P "
                        + "WHERE P.PostID='" + pid + "')";
                java.sql.ResultSet rsIntern3 = stmtIntern3.executeQuery(queryIntern3);
                
                rsIntern3.next();
                data[0] = rsIntern3.getString(1);
                data[1] = d;
                
                hn.add(new DBClasses.HistoryNode(4, data, d));
            }
            
            while (rs5.next()) {
                String d = rs5.getString(3);
                String[] data = new String[2];
                //Data Stored: Comment Author, Date
                String cid = rs5.getString(1);
                
                java.sql.Statement stmtIntern4 = conn.createStatement();
                String queryIntern4 = "SELECT CONCAT(U.Firstname, ' ', U.Lastname) "
                        + "FROM Customer U "
                        + "WHERE U.CustomerID IN "
                        + "(SELECT C.Author "
                        + "FROM Comments C "
                        + "WHERE C.CommentID='" +cid +"')";
                java.sql.ResultSet rsIntern4 = stmtIntern4.executeQuery(queryIntern4);
                
                rsIntern4.next();
                
                data[0] = rsIntern4.getString(1);
                data[1] = d;
                
                hn.add(new DBClasses.HistoryNode(5, data, d));
            }
            

            Collections.sort(hn);
            
        
        %>
        <table>
            <tr><th><%=fname + " " +lname +"'s Public Profile"%></th></tr>
            <%
            for (int i = 0; i < hn.size(); i++) {
                DBClasses.HistoryNode hNode = hn.get(i);
                int type = hNode.getType();
                String[] inf = hNode.getInfo();
                String rowStr = new String();
                if (type==0) {
                        rowStr = fname + " created the circle " + inf[0] +"[" +inf[1] + "] on " + inf[2];
                }
                else if (type==1) {
                    rowStr= fname + " joined " + inf[2] + "'s circle " + inf[0] + "[" + inf[1] + "] on " + inf[3];
                }
                else if (type==2) {
                    rowStr= fname + " wrote " + inf[0] + " on the page for " + inf[2] + "'s circle " + inf[1] + "on " + inf[3];
                }
                else if (type==3) {
                    rowStr = fname + " wrote " + inf[0] + " on " + inf[1] + "'s post on the page for " + inf[2] + " on " + inf[3];
                }
                else if (type==4) {
                    rowStr = fname + " liked " + inf[0] + "'s post on " + inf[1];
                }
                else if (type==5) {
                    rowStr = fname + " liked " + inf[0] + "'s comment on " + inf[1];
                }
                %>
                <tr><td><%=rowStr%></td></tr>
                <%
            }
            %>
        </table>
        <%}
        
        catch(Exception e) {
            e.printStackTrace();
            response.sendRedirect("gohome.jsp");
        }
         finally
                   {
                       try{
                       conn.close(); }
                       catch(Exception ee) {
                           ee.printStackTrace();
                           response.sendRedirect("gohome.jsp");
                       }
                   }
%>
    </body>
</html>
